package com.wushi.lenovo.asia5b.app_ui.profile.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.utils.ResString;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.profile.AddressModel;

import java.util.List;

/**
 * Created by zhangyuncai on 2018/9/29.
 */
public class ShipAddressAdapter extends BaseQuickAdapter<AddressModel, BaseViewHolder> {

    public ShipAddressAdapter(@Nullable List<AddressModel> data) {
        super(R.layout.item_shipaddress_adapter, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, AddressModel item) {

        String shipClassName = ResString.get(R.string.shipaddress_shipclassname);
        String billClassName = ResString.get(R.string.shipaddress_billclassname);
        String allClassName = ResString.get(R.string.shipaddress_alladdress);

        int position = helper.getLayoutPosition();

        //分类栏
        TextView tv_ship_class = helper.getView(R.id.tv_ship_class);
        //内容布局-空布局
        TextView tv_ship_empty = helper.getView(R.id.tv_ship_empty);
        //内容布局-非空布局
        LinearLayout ll_ll_ship_content = helper.getView(R.id.ll_ll_ship_content);
        //分割线
        View view_ship_divider = helper.getView(R.id.view_ship_divider);

        //是否默认
        TextView tv_ship_default = helper.getView(R.id.tv_ship_default);

        tv_ship_class.setVisibility(position < 3 ? View.VISIBLE : View.GONE);
        view_ship_divider.setVisibility(View.GONE);
        tv_ship_empty.setVisibility(item.getId() <= 0 ? View.VISIBLE : View.GONE);
        ll_ll_ship_content.setVisibility(item.getId() > 0 ? View.VISIBLE : View.GONE);
        if (position == 0) {//ship
            tv_ship_class.setText(shipClassName);
            tv_ship_default.setVisibility(View.VISIBLE);
        } else if (position == 1) {//bill
            tv_ship_class.setText(billClassName);
            tv_ship_default.setVisibility(View.VISIBLE);
        } else {
            tv_ship_class.setText(allClassName);
            tv_ship_default.setVisibility(View.INVISIBLE);

            if (position != (mData.size() - 1)) {
                view_ship_divider.setVisibility(View.VISIBLE);
            }
        }
        if (item.getId() > 0) {
            setAddressView(helper, item);
        }


    }

    private void setAddressView(BaseViewHolder helper, AddressModel item) {
        int position = helper.getLayoutPosition();


        //标签名
        if (TextUtils.isEmpty(item.getBuilding_name())) {
            helper.setGone(R.id.tv_ship_buildingname, false);
        } else {
            helper.setGone(R.id.tv_ship_buildingname, true);
            helper.setText(R.id.tv_ship_buildingname, item.getBuilding_name());
        }

        //姓名
        helper.setText(R.id.tv_ship_username, item.getContact_name());


        //地址名称
        helper.setText(R.id.tv_ship_address,
                item.getLocalAddressDetail()
        );

        //手机号
        String phone = String.format("+%s%s", item.getCalling_code(), item.getContact_no());
        helper.setText(R.id.tv_ship_phone, phone);

        //编辑
        helper.addOnClickListener(R.id.iv_ship_edit);
        helper.addOnClickListener(R.id.iv_ship_delete);
    }


}